<?php
include_once dirname(__FILE__).'/Rectangle.class.php';

class Transform {
	
	/**
	 * Put graphic object in space
	 * 
	 * @param $origSize array original size array([widht],[height])
	 * @param $newSize array new size array([widht],[height])
	 * @param $cropp boolean 
	 * @param $centred boolean 
	 * 
	 * @return Rectangle
	 */
	static public function putInSpace(array $origSize, array $newSize, $cropp = true, $centred = true){
		$x_ratio = $newSize[0] / $origSize[0];
		$y_ratio = $newSize[1] / $origSize[1];
		
		if ($cropp == true)
			$ratio = max($x_ratio, $y_ratio);
		else
			$ratio = min($x_ratio, $y_ratio);
		
		$use_ratio = ($x_ratio == $ratio);
		
		$new_width = $use_ratio ? $newSize[0]:floor($origSize[0]*$ratio);
		$new_height = !$use_ratio ? $newSize[1]:floor($origSize[1]*$ratio);
		if ($centred == true) {
			$new_x = $use_ratio?0:floor(($newSize[0]-$new_width)/2);
			$new_y = !$use_ratio?0:floor(($newSize[1]-$new_height)/2);
		}
		
		if ($centred == false)
			return new Rectangle(0, 0, $new_width, $new_height);
		else
			return new Rectangle($new_x, $new_y, $new_width, $new_height);
	}
}
?>